package juc2.synchronizeddome;

/**
 * 修饰代码块（其实还是修饰的对象）
 */
public class 修饰代码块 {
    //自定义锁对象
    private final Object look = new Object();
    private int count = 0;

    public void increment() {
        synchronized (look) {
            System.out.println(Thread.currentThread().getName() + " is running");
            count++;
            System.out.println(Thread.currentThread().getName() + " count: " + count);
        }
    }

    /**
     * 错误用法
     * @param args
     */
   /* public static void main(String[] args) {
        System.out.println(Thread.currentThread().getName() + " is running");
        for (int i = 0; i < 10; i++) {
            new Thread(() -> {
                new 修饰代码块().increment();//新建一个修饰代码块实例，这会导致锁对象不共享
            },"Thread-"+i).start();
        }
    }*/
    /**
     * 正确的用法
     * @param args
     */
    public static void main(String[] args) {
        System.out.println(Thread.currentThread().getName() + " is running");
        修饰代码块 修饰代码块 = new 修饰代码块();
        for (int i = 0; i < 10; i++) {
            //新建一个修饰代码块实例，这会导致锁对象不共享
            new Thread(修饰代码块::increment,"Thread-"+i).start();
        }
    }
}
